import 'package:better_player_plus/better_player_plus.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:yqmssa_app/config/app_config.dart';
import 'package:yqmssa_app/utils/shared_utils.dart';
import 'package:yqmssa_app/utils/tencent_im_utils.dart';
import 'package:yqmssa_app/views/index/request/index_request.dart';
import 'package:yqmssa_app/views/index/screen/message_manager.dart';
import 'package:yqmssa_app/views/message/chat_method.dart';

import 'apply_request.dart';
import 'sheet_screen.dart';

class LandingManager extends GetxController {
  @override
  onInit() {
    super.onInit();
    gD();
    gQ();
  }

  final data = VideoDetailDataModel().obs;

  final queryData = Get.parameters;

  late CreateApplyDataModel applyDataModel;

  late BetterPlayerController betterPlayerController;

  final range = ["1-5万", "5-20万", "20-30万", "30-80万", "80万以上"];

  final rangeType = ["信用卡", "网贷", "企业贷", "信用贷", "其他"];

  final selRange = [].obs;

  final selRangeType = [].obs;

  void gD() {
    ApplyRequest.getVI(int.parse(queryData["id"] ?? "0")).then((value) {
      data.value = value;
    });
  }

  void gQ() {
    ApplyRequest.getQuestion().then((value) {
      // data.value.question = value;
    });
  }

  void select({
    int type = 0,
    String value = "",
  }) {
    if (type == 0) {
      if (selRange.contains(value)) {
        selRange.remove(value);
      } else {
        selRange.add(value);
      }
    }
    if (type == 1) {
      if (selRangeType.contains(value)) {
        selRangeType.remove(value);
      } else {
        selRangeType.add(value);
      }
    }
  }

  void showSheet() async {
    betterPlayerController.pause();
    if (sharedUtils.service != null) {
      gotoChat();
      return;
    }
    final res = await Get.bottomSheet(const SheetScreen(), isScrollControlled: true);
    if (res != null && res) {
      gotoChat();
    } else {
      betterPlayerController.play();
    }
  }

  ///提交线索
  void putApply() {
    if (selRangeType.isEmpty) {
      SmartDialog.showToast("请选择债务类型");
      return;
    }
    if (selRange.isEmpty) {
      SmartDialog.showToast("请选择债务总金额");
      return;
    }
    final parameters = {
      'video_id': data.value.video?.id ?? 0,
      'debt_range': selRangeType.join(","),
      'money_range': selRange.join(","),
      "overdue_duration": "",
      "username": "",
      "other_touch": "",
      "mobile_time": "",
      "admin_last_id": data.value.adminId
    };
    SmartDialog.showLoading(msg: "正在提交申请");
    ApplyRequest.plan(parameters).then((value) async {
      applyDataModel = value;
      sharedUtils.service = value;
      sendIm();
      SmartDialog.dismiss();
      SmartDialog.showToast("已收到你的申请，稍后会有法务专员联系您");
      Get.back(result: true);
    }).catchError((err) {
      SmartDialog.dismiss();
      SmartDialog.showToast(err.message ?? "");
      gD();
    });
  }

  ///发送第一条Im信息
  void sendIm() {
    ChatMethods.sendMessage(AppConfig.appInitData.chatBot == 1 ? "@customer_service_account" : applyDataModel.serviceTencentId);
  }

  ///跳转微信并且跳转聊天
  void gotoChat() {
    tencentImUtils.pushPage(false);
    Get.find<MessageManager>().getSystemMsg();
  }
}
